package com.guide.mvc;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.guide.bizdto.CommentsParam;
import com.guide.bizdto.MessageParam;
import com.guide.bizdto.MessageSel;
import com.guide.bizdto.TravelTeamParam;
import com.guide.bizdto.TravelTeamSel;
import com.guide.bizdto.UserInfoParam;
import com.guide.dao.PersonalDao;
import com.guide.domain.Account;
import com.guide.domain.Guide;
import com.guide.domain.Travelorg;
import com.guide.dto.RetDTO;
import com.guide.enums.ErrorCodeEnum;
import com.guide.exception.ServiceException;
import com.guide.service.AccountService;
import com.guide.service.CommentsService;
import com.guide.service.GuideService;
import com.guide.service.TravelTeamService;
import com.guide.utils.ConstantUtils;
import com.guide.utils.QiNiuUtils;

@Controller
@RequestMapping(value = { "/core","/open","/"}, produces = "application/json")
public class MsgController extends AbstractController{

	@Autowired
	private AccountService accountService;
	@Autowired
	private TravelTeamService travelTeamService;
	@Autowired
	private GuideService guideService;
/*	@Autowired
	private CommentsService commentsService;*/
	
	
	
	@RequestMapping("/sendLetter")
	public RetDTO sendLetter(@RequestBody MessageParam messageParam) {
		Account recAccount = accountService.getAccountByUserName(messageParam.getRecUserName());
		if(recAccount==null) {
			throw new ServiceException(ErrorCodeEnum.CODE_GUIDE_USERNMAE_NOTEXIST_ERROR);
		}
		Account account = super.getCurrAccount();
		commentsService.sendPrivateLetter(messageParam, accountService.getAccountDetailByAccount(account),accountService.getAccountDetailByAccount(recAccount));
		refreshUnreadCount();
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/sendSysLetter")
	public RetDTO sendSysLetter(@RequestBody MessageParam messageParam) {
	
		Account account = super.getCurrAccount();
		commentsService.sendSysLetter(messageParam, account);
		refreshUnreadCount();
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	@RequestMapping("/replyLetter")
	public RetDTO replyLetter(@RequestBody MessageParam messageParam) {
		Account recAccount = accountService.getAccountById(Long.valueOf(messageParam.getRecId()));
		Account account = super.getCurrAccount();
		commentsService.sendPrivateLetter(messageParam, accountService.getAccountDetailByAccount(account),accountService.getAccountDetailByAccount(recAccount));
		refreshUnreadCount();
		RetDTO retDTO = new RetDTO();
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getLetters")
	public RetDTO getLetters(@RequestBody MessageSel messageSel) {
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = commentsService.getLetters(messageSel, account);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getSysLetters")
	public RetDTO getSysLetters(@RequestBody MessageSel messageSel) {
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		Map<String,Object> retMap = commentsService.getLettersSys(messageSel, account);
		retDTO.addResult(ConstantUtils.DATE_LIST, retMap.get(ConstantUtils.DATE_LIST));
		retDTO.addResult(ConstantUtils.TOTAL_COUNT, retMap.get(ConstantUtils.TOTAL_COUNT));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getLetterById")
	public RetDTO getLetterById(@RequestParam Long messageId) {
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		retDTO.addResult("messageData", commentsService.getLetterById(messageId));
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/markRead")
	public RetDTO markRead(@RequestParam Long messageId) {
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		commentsService.markRead(messageId);
		refreshUnreadCount();
		retDTO.addResult("result", "ok");
		retDTO.markSuccess();
		return retDTO;
	}
	
	@RequestMapping("/getUnreadLetterCount")
	public RetDTO getUnreadLetterCount(@RequestParam Integer status) {
		RetDTO retDTO = new RetDTO();
		Account account = this.getCurrAccount();
		if(status==null) {
			status = ConstantUtils.MSG_READ_0;
		}
		retDTO.addResult("unReadCount", commentsService.getUnreadLetterCount(account.getUserId(), status));
		retDTO.markSuccess();
		return retDTO;
	}
	
	
	
	
	
	
	
	
	

}
